﻿using IQIGame.Onigao.Game;
using LFloatMath.Math;
using NetProtocol.POD;

namespace IQIGame.Onigao.GamePlay
{
    public static class UnitViewFactory
    {
        public static UnitView CreateBattleUnitView(BattleUnitViewPOD rPOD, int nTeamIndex)
        {
            var rUnit = new UnitView()
            {
                GUID = rPOD.guid,
                configID = rPOD.configID,
                teamIndex = nTeamIndex,
                unitType = (EUnitType)rPOD.unitType,
                skinID = rPOD.skinID
            };
            var rUnitProperty = new UnitViewProperty();
            if (rUnit.unitType == EUnitType.Hero)
            {
                var rConfig = TableCenter.hero.Get(rUnit.configID);
                rUnitProperty.radius = rConfig.Radius / 100f;
                if (rUnit.skinID == 0)
                {
                    rUnit.skinID = rConfig.HeroSkinIds[0];
                }
                var nSkinID = rUnit.skinID;
                var rSkinConfig = TableCenter.heroSkin.Get(nSkinID);
                rUnitProperty.roleName = rSkinConfig.BattleEntityAsset;
                rUnitProperty.resPath = rSkinConfig.BattleEntityAsset_fullPath;
                rUnitProperty.entityType = (int)EUnitType.Hero;
                rUnitProperty.battleHeadIconPath = rSkinConfig.BattleHeadIcon_fullPath;
                rUnitProperty.dialog = rConfig.Diglog;
                rUnitProperty.name = rConfig.Name;
                rUnitProperty.widgetPath = PathConstant.GetHeroWidgetPath(rUnitProperty.roleName, rSkinConfig.WidgetEntityAsset);
            }
            else
            {
                var rConfig = TableCenter.monster.Get(rUnit.configID);
                rUnitProperty.roleName = rConfig.EntityAsset;
                rUnitProperty.resPath = rConfig.EntityAsset_fullPath;
                rUnitProperty.entityType = (int)EUnitType.Monster;
                rUnitProperty.radius = rConfig.Radius / 100f;
                rUnitProperty.weaknessTypeList = rConfig.WeakType;
                rUnitProperty.battleHeadIconPath = rConfig.BattleHeadIcon_fullPath;
                rUnitProperty.dialog = rConfig.Diglog;
                rUnitProperty.name = rConfig.Name;
            }

            rUnit.property = rUnitProperty;

            var rAttr = rUnit.AddComponent<AttributeViewComponent>(ETComponentType.AttributeViewComponent);
            rUnit.AddComponent<UnitViewComponent>(ETComponentType.UnitViewComponent);
            rUnit.AddComponent<BattleAnimationComponent>(ETComponentType.BattleAnimationComponent);
            rUnit.AddComponent<BattleMoveComponent>(ETComponentType.BattleMoveComponent);
            rUnit.AddComponent<EffectComponent>(ETComponentType.EffectComponent);
            var rSkill = rUnit.AddComponent<SkillViewComponent>(ETComponentType.SkillViewComponent);
            rUnit.AddComponent<BattleViewBindPointComponent>(ETComponentType.BattleViewBindPointComponent);
            rUnit.AddComponent<CustomPropertyViewComponent>(ETComponentType.CustomPropertyViewComponent);
            rUnit.AddComponent<BuffViewComponent>(ETComponentType.BuffViewComponent);
            rUnit.AddComponent<BattleTimelineComponent>(ETComponentType.BattleTimelineComponent);
            rUnit.AddComponent<BulletComponent>(ETComponentType.BulletComponent);
            var rColComp = rUnit.AddComponent<CollisionViewComponent>(ETComponentType.CollisionViewComponent);
            // AI开关在AIComponent中，加上此Component为了保证随时可以动态手操单位
            rUnit.AddComponent<PlayerOrderComponent>(ETComponentType.PlayerOrderComponent);
            rUnit.AddComponent<ChargeViewComponent>(ETComponentType.ChargeViewComponent);
            rUnit.AddComponent<BattleImpactComponent>(ETComponentType.BattleImpactComponent);
            var rPetComp = rUnit.AddComponent<PetViewComponent>(ETComponentType.PetViewComponent);
            
            rSkill.Initialize(rPOD.skill);
            rAttr.Initialize(rPOD.attr);
            rColComp.InitializeCircle(rUnit.radius.ToLFloat()); // 厘米转米
            rPetComp.Initialize(rPOD.pet);
            
            rUnit.pos = rPOD.pos.ToLV2();
            rUnit.forward = rPOD.forward.ToLV2();
            rUnit.maxWeaknessPoint = rPOD.weaknessPoint;
            rUnit.weaknessType = EDamageType.None;
            if (rUnit.property.weaknessTypeList != null)
            {
                for (int i = 0; i < rUnit.property.weaknessTypeList.Count; i++)
                {
                    rUnit.weaknessType |= (EDamageType)(1 << rUnit.property.weaknessTypeList[i]);
                }
            }
            return rUnit;
        }
        
        public static TotemView CreateBattleTotemView(TotemResponse resp, int teamIndex, UnitView summoner)
        {
            var rUnitPod = resp.unit;
            var rTotem = new TotemView()
            {
                GUID = rUnitPod.guid,
                teamIndex = teamIndex,
                unitType = EUnitType.Totem,
                summoner = summoner
            };
            var rViewComp = rTotem.AddComponent<TotemViewComponent>(ETComponentType.TotemViewComponent);
            rTotem.AddComponent<EffectComponent>(ETComponentType.EffectComponent);
            rTotem.AddComponent<BattleViewBindPointComponent>(ETComponentType.BattleViewBindPointComponent);
            rTotem.AddComponent<BuffViewComponent>(ETComponentType.BuffViewComponent);
            
            rTotem.pos = rUnitPod.pos.ToLV2();
            rTotem.forward = LVector2.zero;
            
            rViewComp.Initialize(resp.effectName);
            return rTotem;
        }
    }
}